using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using Elmah;

namespace SurveyHost.Core
{
    /// <summary>
    /// An exception class used to wrap information for logging to the Elmah package
    /// </summary>
    [Serializable]
    public class SurveyHostException : Exception
    {
        //
        // For guidelines regarding the creation of new exception types, see
        //    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconerrorraisinghandlingguidelines.asp
        // and
        //    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol/html/csharp07192001.asp
        //

        public SurveyHostException()
        {
        }

        public SurveyHostException(string message)
            : base(message)
        {
        }

        public SurveyHostException(string message, Exception inner)
            : base(message, inner)
        {
        }

        protected SurveyHostException(
            SerializationInfo info,
            StreamingContext context)
            : base(info, context)
        {
        }
    }

    /// <summary>
    /// Helper methods for logging
    /// </summary>
    public static class LogHelper
    {
        /// <summary>
        /// Logs an exception
        /// </summary>
        /// <param name="ex">The exception to log.</param>
        /// <param name="message">The message to write with the exception.</param>
        public static void Exception(Exception ex, string message)
        {
            ErrorSignal.FromCurrentContext().Raise(new SurveyHostException(message, ex));
                //.Write(message, EXCEPTIONLOG, GenerateExceptionDictionary(ex));
        }

        /// <summary>
        /// Logs an exception
        /// </summary>
        /// <param name="ex">The exception to log.</param>
        /// <param name="format">The format message string.</param>
        /// <param name="args">The arguments for the format string.</param>
        public static void Exception(Exception ex, string format, params object[] args)
        {
            ErrorSignal.FromCurrentContext().Raise(new SurveyHostException(string.Format(format, args), ex));
        }

        /// <summary>
        /// Writes a log entry to the general log
        /// </summary>
        /// <param name="message">The message to write.</param>
        public static void General(string message)
        {
            ErrorSignal.FromCurrentContext().Raise(new SurveyHostException(message));
        }

        /// <summary>
        /// Writes a log entry to the general log
        /// </summary>
        /// <param name="message">The formatted message to write</param>
        /// <param name="args">The format args for the message</param>
        public static void General(string message, params object[] args)
        {
            General(string.Format(message, args));
        }
    }
}
